package org.janon.httpproxy;

public class HttpURL
{
	private String host = "";
	private String path = "";
	private int port = -1;
	private String query = "";
	private String url = "";

	public HttpURL(String url, int methodId)
	{
		this.url = url;
		
		if(methodId == HttpUtil.HTTP_CONNECT_CONTINUE)
		{
			return;
		}
		
		if (methodId == HttpUtil.HTTP_CONNECT)
		{
			port = 443;
		}

		if (HttpUtil.startsWith(url, "HTTP://"))
		{
			port = 80;
			url = url.substring(7);
		}
		else if (HttpUtil.startsWith(url, "HTTPS://"))
		{
			port = 443;
			url = url.substring(8);
		}

		int a = url.indexOf("/");
		if (a == -1)
		{
			a = url.length();
		}
		host = url.substring(0, a);
		if (a != url.length())
			url = url.substring(a);
		else
			url = "";

		a = host.indexOf(":");
		if (a != -1)
		{
			port = Integer.parseInt(host.substring(a + 1));
			host = host.substring(0, a);
		}

		a = url.indexOf("?");
		if (a == -1)
		{
			a = url.length();
		}
		path = url.substring(0, a);
		if (a != url.length())
			query = url.substring(a + 1);

	}
//
//	public static void main(String[] args)
//	{
//		new HttpURL("passport.baidu.com:443", 1);
//	}

	public String getHost()
	{
		return host;
	}
	
	public void setHost(String host)
	{
		this.host = host;
	}

	public String getPath()
	{
		return path;
	}

	public int getPort()
	{
		return port;
	}
	
	public void setPort(int port)
	{
		this.port = port;
	}

	public String getQuery()
	{
		return query;
	}

	public String getUrl()
	{
		return url;
	}
}
